﻿using lwzlz.Application.System;
using lwzlz.Common;
using lwzlz.Web.Areas.SysAdmin.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace lwzlz.Web.Areas.SysAdmin.Controllers
{
    public class HomeController : AdminController
    {
        #region 首页
        public ActionResult Index()
        {
            var user = UserAuthentication.GetCurrentUser();
            var topMenus = new AuthorityService().GetAuthorityList()
                .Where(m => m.MenuDeep == 0).OrderBy(m => m.SortIndex)
                .Select(m => new LayMenuModel()
                {
                    title = m.MenuName,
                    icon = m.MenuIcon
                });

            ViewBag.TopMenus = topMenus;
            ViewBag.User = UserAuthentication.GetCurrentUser();
            return View();
        }
        #endregion

        #region 获取用户可见菜单
        public JsonResult GetMenus()
        {
            IDictionary<string, IEnumerable<LayMenuModel>> menus =
                new Dictionary<string, IEnumerable<LayMenuModel>>();

            AuthorityService service = new AuthorityService();
            try
            {
                // 获取权限菜单列表
                //var menuList = service.GetAuthorityMenus();
                var menuList = service.GetAuthorityMenus().Where(m => m.MenuName != "老师列表").ToList();
                if (menuList != null && menuList.Count() > 0)
                {
                    // 获取顶部一级菜单
                    var topMenus = menuList.Where(m => m.MenuDeep == 0);
                    if (topMenus != null && topMenus.Count() > 0)
                    {
                        foreach (var item in topMenus)
                        {
                            // 获取一级菜单下菜单列表（二级菜单）
                            IEnumerable<LayMenuModel> list = menuList.Where(m => m.ParentId == item.Id).OrderBy(m => m.SortIndex)
                            .Select(m => new LayMenuModel()
                            {
                                title = m.MenuName,
                                href = m.MenuUrl,
                                icon = m.MenuIcon,
                                // 三级菜单
                                children = menuList.Where(c => c.ParentId == m.Id).OrderBy(c => c.SortIndex)
                                        .Select(c => new LayMenuModel()
                                        {
                                            title = c.MenuName,
                                            href = c.MenuUrl,
                                            icon = c.MenuIcon,
                                        })
                            });
                            // 添加一级菜单下目录
                            menus.Add(item.MenuName, list);
                        }
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
            return Json(menus, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 清除缓存
        public JsonResult ClearCache()
        {
            ResponseModel response = new ResponseModel();
            try
            {
                // 删除用户权限菜单Session
                ZSession.Remove("UserMenuPower");

                // TODO: 清除其他

                response.Success = true;
                response.Message = "清除成功";
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }

            return Json(response);
        }
        #endregion
    }
}